package com.ht.module.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.constant.BusConstant;
import com.ht.constant.DicConstants;
import com.ht.module.bus.entity.TRoute;
import com.ht.module.bus.entity.TStation;
import com.ht.module.bus.entity.TVehicleRouteResponsibility;
import com.ht.module.bus.mapper.TRouteMapper;
import com.ht.module.bus.mapper.TStationMapper;
import com.ht.module.bus.service.TRouteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.util.Pager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 线路信息表，是使用线路沙盘功能规划的线路信息保存表 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2024-07-29
 */
@Service
public class TRouteServiceImpl extends ServiceImpl<TRouteMapper, TRoute> implements TRouteService {
    @Resource
    private TStationMapper stationMapper;

    @Override
    public Pager<TRoute> findPage(Pager<TRoute> pager, TRoute en) {
        QueryWrapper<TRoute> qw = getQW((en));
        baseMapper.findPageAuth(pager,qw,null);
        translateStation(pager.getRecords());
        return pager;
    }

    @Override
    public List<TRoute> findList(TRoute en) {
        QueryWrapper<TRoute> qw = getQW((en));
        qw.eq("tab.state", DicConstants.State.ENABLE);
        List<TRoute> tRoutes = baseMapper.findListAuth(qw,null);
        translateStation(tRoutes);
        return tRoutes;
    }

    /**
     * 翻译站点
     *
     * @param list
     */
    private void translateStation(List<TRoute> list) {
        //查询所有站点
        List<TStation> tStations = stationMapper.findListAuth(new QueryWrapper<>(),null);
        for (TRoute tRoute : list) {
            //获取起点
            tRoute.setStationCodeStartEn(
                    tStations.stream()
                            .filter(x -> StrUtil.equals(tRoute.getStationCodeStart(), x.getStationCode()))
                            .findFirst().orElse(null)
            );
            //获取终点
            tRoute.setStationCodeEndEn(
                    tStations.stream()
                            .filter(x -> StrUtil.equals(tRoute.getStationCodeEnd(), x.getStationCode()))
                            .findFirst().orElse(null)
            );

            //构建途经点
            List<String> codes = Arrays.asList(tRoute.getPassingPoindCode().split(","));
            tRoute.setPassingPoindCodeEns(
                    codes.stream().map(y ->
                            tStations.stream()
                                    .filter(x -> StrUtil.equals(y, x.getStationCode()))
                                    .filter(BeanUtil::isNotEmpty)
                                    .findFirst().orElse(null)
                    ).collect(Collectors.toList())
            );
        }


    }


    private QueryWrapper<TRoute> getQW(TRoute en){
        QueryWrapper<TRoute> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(en.getRouteName())){
            qw.like("tab.route_name",en.getRouteName());
        }
        if(StrUtil.isNotEmpty(en.getState())){
            qw.eq("tab.state",en.getState());
        }
        qw.eq("tab.del_flag", BusConstant.DEL_FLAG_N);
        qw.orderByDesc("create_date");
        return qw;
    }
}
